package com.simtop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.simtop.dao.ExperimentMapper;
import com.simtop.expection.GlobalException;
import com.simtop.pojo.Sqsy;
import com.simtop.pojo.experiment.Experiment;
import com.simtop.pojo.experiment.ExperimentDynamicVo;
import com.simtop.pojo.experiment.ExperimentReportVo;
import com.simtop.service.CheckContent;
import com.simtop.service.ExperimentService;
import com.simtop.util.html.EscapeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lvyongwei
 */
@Service
public class ExperimentServiceImpl implements ExperimentService {

    @Autowired
    private ExperimentMapper experimentMapper;

    @Autowired
    private CheckContent checkContent;

    /**
     * 添加实验报告
     * @param experiment
     * @return
     */
    @Override
    public boolean insertExperiment(Experiment experiment) {
        return experimentMapper.insertExperiment(experiment);
    }

    /**
     * 审核实验报告内容
     * @param experiment
     * @return
     */
    @Override
    public List<String> checkExperiment(Experiment experiment) {
        String clean = EscapeUtil.clean(experiment.getExperimentContent());
        int a = clean.length()/6666;
        if(clean.length()/6666!=0){
            a = a+1;
        }
        ArrayList<String> strings = new ArrayList<>(a);
        //判断是否超过审核的最大限制
        if(clean.length() > 6666){
            for (int i = 1; i <= a; i++) {
                if(i == a){
                    String b = checkContent.checkText(clean.substring((i-1)*6666,clean.length()));
                    if(b == null){
                        return null;
                    }
                    strings.add(b);
                }else{
                    String s = checkContent.checkText(clean.substring((i-1)*6666,i*6666));
                    if(s == null){
                        return null;
                    }
                    strings.add(s);
                }
            }
        }else{
            String s = checkContent.checkText(clean);
            if(s == null){
                return null;
            }
            strings.add(s);
        }
        for (String s : strings) {
            JSONObject parse = JSONObject.parseObject(s);
            if(!parse.getString("conclusionType").equals("1") && parse.getString("conclusion").equals("不合规")){
                throw new GlobalException("审核提交的内容失败",strings);
            }
        }
        return strings;
    }

    /**
     * 修改实验报告
     * @param experiment
     * @return
     */
    @Override
    public boolean updateExperiment(Experiment experiment) {
        return experimentMapper.updateExperiment(experiment);
    }

    /**
     * 作品动态列表
     * @param message 关键字
     * @return
     */
    @Override
    public List<ExperimentDynamicVo> getAllDynamic(String message) {
        return experimentMapper.getAllDynamic(message);
    }

    /**
     * 获取实验报告列表
     * @param userId
     * @param message 关键字
     * @return
     */
    @Override
    public List<ExperimentReportVo> getAllReport(Integer userId, String message) {
        return experimentMapper.getAllReport(userId,message);
    }

    /**
     * 点赞
     * @param userId
     * @param experimentId
     * @return
     */
    @Override
    public boolean giveLike(Integer userId, Long experimentId) {
        return experimentMapper.giveLike(userId, experimentId);
    }

    /**
     * 取消点赞
     * @param userId
     * @param experimentId
     * @return
     */
    @Override
    public boolean unGiveLike(Integer userId, Long experimentId) {
        return experimentMapper.unGiveLike(userId, experimentId);
    }

    /**
     * 删除实验报告
     * @param experimentId
     * @return
     */
    @Override
    public boolean delete(Long experimentId) {
        return experimentMapper.delete(experimentId);
    }

    @Override
    public void tjcj(Integer sqsyuserid, String sqsyname, String sqsyxq, String sqsypj, String sqsysc, int sqsyzf, String sqsysj) {
        experimentMapper.tjcj(sqsyuserid,sqsyname,sqsyxq,sqsypj,sqsysc,sqsyzf,sqsysj);
    }

    @Override
    public List<Sqsy> allsy(int userid) {
        return experimentMapper.allsy(userid);
    }

    @Override
    public List<Sqsy> allallsy() {
        return experimentMapper.allallsy();
    }

    @Override
    public void deleteExperimental(int experimentId) {
         experimentMapper.deleteExperimental(experimentId);
    }
}
